package controller;

import java.net.InetAddress;
import java.net.UnknownHostException;

import ni.P2PNI;
import gui.P2PGUI;
import gui.User;

public class P2PController {
 
	private P2PGUI gui;
	private P2PNI ni;
	private String user_name;
	
	public P2PController(){
		
	}

	public void performConnect(String user_name){
		this.user_name = user_name;
		
		this.ni = new P2PNI(this);
		ni.sendHello(user_name);
	}
	
	public void performDisconnect(String user_name){
		ni.sendGoodBye(user_name);
		ni.close_sockets();
	}
	
	public void performAnswer(String messageToSend, User user){
		ni.sendMessage(messageToSend,user);
		gui.addToListOfMessages(messageToSend, user);
	}
	
	
	public void processHelloAck(User connectedUser){
        gui.addToUserList(connectedUser);
	}

	public void processHello(User newUser){
		gui.addToUserList(newUser);
		ni.sendHelloAck(newUser, user_name);
	}

	public void processGoodBye(InetAddress ip){
		gui.removeFromUserList(ip);
	}
	
	public void processMessage(String ip, String messageData, int i) {
		try {
			gui.addToListOfMessages(ip, messageData);
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void processMessageAck(int messageNumber) {
        System.out.println("Dans le Controller : On a reçu le ack numero " + messageNumber);
        gui.setACK("Message number" + messageNumber +" received");		
        
	}
	
	public void fileReceivedNotification(String result) {
    	//gui.setACK("FICHIER : " + result); 
    	gui.getConnectedWindow().updateListMessages("FICHIER : " + result);
    	}
    
    

    public void processFileTranferResult(String result) {
       	gui.setACK(result);
    }

	public void performSendFile(String filePath, User user) {
        System.out.println("Controller : Send File to users selected");
        
            String remoteUser = user.getName();
            String UserIp = user.getIpAddress().getHostAddress().toString();
            String sendFileMessage = "You are sending this file to " + remoteUser + " : \n" +filePath + "\n";
            gui.getConnectedWindow().updateListMessages(sendFileMessage);
            ni.sendFile(filePath,UserIp);
        }

	public void processSendmessage(int numero, String ip) {
		gui.setACK("Sending message number " + numero +" à "+ ip);		
	}
	
	// GETTERS & SETTERS
	
	public P2PGUI getGui() {
		return gui;
	}

	public void setGui(P2PGUI gui) {
		this.gui = gui;
	}

	
    
		
	}
	
	

